package com.example.hzq.food.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.hzq.food.R;
import com.example.hzq.food.bean.RegisterBean;
import com.example.hzq.food.model.Model;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hzq on 2017/6/10.
 */

public class Register extends AppCompatActivity implements View.OnClickListener{
    EditText name,pwd,epwd,phone,address,comment;
    Button reg,back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        init();
    }
    private void init(){
        name=(EditText)findViewById(R.id.editText3);
        pwd=(EditText)findViewById(R.id.editText4);
        epwd=(EditText)findViewById(R.id.editText5);
        phone=(EditText)findViewById(R.id.editText6);
        address=(EditText)findViewById(R.id.editText7);
        comment=(EditText) findViewById(R.id.editText8);
        reg=(Button)findViewById(R.id.button3);
        back=(Button)findViewById(R.id.button4);
        reg.setOnClickListener(this);
        back.setOnClickListener(this);
    }
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button3:
                if(name.getText().toString().isEmpty()||pwd.getText().toString().isEmpty()){
                    Toast.makeText(Register.this, "注册失败,请确定是否输入了用户名或密码", Toast.LENGTH_SHORT).show();
                }
                else if(!Objects.equals(pwd.getEditableText().toString(), epwd.getEditableText().toString())){
                    Toast.makeText(Register.this, "注册失败,请确定两次输入的密码相同", Toast.LENGTH_SHORT).show();

                }
                else{
                   Model registerModel=new Model();
                    Call<RegisterBean> call=registerModel.register(name.getText().toString(),
                            pwd.getText().toString(),
                            phone.getText().toString(),
                            address.getText().toString(),
                            comment.getText().toString());
                    call.enqueue(new Callback<RegisterBean>() {
                        @Override
                        public void onResponse(Call<RegisterBean> call, Response<RegisterBean> response) {
                            if (response.isSuccessful() && response.body() != null)  {
                                if(response.body().getSuccess().equals("1")){
                                    Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
                                    Intent intent1=new Intent();
                                    intent1.setClass(Register.this,Login.class);
                                    startActivity(intent1);
                                    finish();
                                }
                                else  Toast.makeText(Register.this,"注册失败！", Toast.LENGTH_SHORT).show();
                            }
                            else{
                                Toast.makeText(Register.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                        @Override
                        public void onFailure(Call<RegisterBean> call, Throwable t) {
                            Toast.makeText(Register.this, "链接失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.button4:
                Toast.makeText(Register.this,"返回登录",Toast.LENGTH_SHORT).show();
                Intent intent=new Intent();
                intent.setClass(Register.this,Login.class);
                startActivity(intent);
                finish();
                break;
            default:break;
        }
    }
}
